import {
	ref,
	onMounted
} from 'vue';

const shareUrl = ref('');

onMounted(() => {
	uni.showShareMenu({
		withShareTicket: true
	});
});

// 分享到好友
const handleShareAppMessage = (res) => {
	const pages = getCurrentPages()
	const currentPage = pages[pages.length - 1]
	const route = currentPage.route
	const query = currentPage.options || {}

	const queryString = Object.keys(query)
		.map(key => `${key}=${encodeURIComponent(query[key])}`)
		.join('&')

	const fullPath = `/${route}${queryString ? '?' + queryString : ''}`
	if (res.from === 'button' || res.from === 'menu') {
		return {
			title: '渝快聘',
			path: fullPath,
			imageUrl: shareUrl.value
		};
	}
	return {};
};


// 分享到朋友圈
const handleShareTimeline = (res) => {
	const pages = getCurrentPages()
	const currentPage = pages[pages.length - 1]
	const route = currentPage.route
	const query = currentPage.options || {}
	
	const queryString = Object.keys(query)
		.map(key => `${key}=${encodeURIComponent(query[key])}`)
		.join('&')
	
	const fullPath = `/${route}${queryString ? '?' + queryString : ''}`
	if (res.from === 'button' || res.from === 'menu') {
		return {
			title: '渝快聘',
			path: fullPath,
			imageUrl: shareUrl.value
		};
	}
	return {};
};

// 导出配置，供外部使用（如页面配置）
export default {
	setup() {
		// 这里可以返回一些需要在模板中使用的响应式数据或函数
		return {
			// 可以将handleShareAppMessage和handleShareTimeline直接暴露给模板使用（如果需要的话）
			// 但通常分享逻辑是通过页面的onShareAppMessage和onShareTimeline钩子处理的，所以这里不需要返回
		};
	},
	onShareAppMessage: handleShareAppMessage,
	onShareTimeline: handleShareTimeline
};